let express = require("express")();
let expressStatic = require("express-static");
let mysql = require("mysql");
//接受post请求的请求参数
let bodyParse = require("body-parser");
const port = 8080;

//为body-parse配置
express.use(bodyParse.json());
express.use(bodyParse.urlencoded({ extended: false }));

//数据库配置
var sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'vivo',
    timezone: "08:00",
});
sql.connect();

//路由请求

//登录接口
express.post("/login", (request, response) => {
    //接受post请求的请求参数
    let q = request.body;
    sql.query(`select * from user where username="${q.username}" and password="${q.password}"`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("0");
        } else {
            if (data.length === 0) {
                response.send("1");
            } else {
                response.send(JSON.stringify(data));
                console.log(`${q.username}用户登录成功`);
            }
        }
    })
})

//注册接口
express.post("/register", (request, response) => {
    let q = request.body;
    sql.query(`insert into user (username,password) values ("${q.username}","${q.password}")`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            response.send("success");
            console.log(`${q.realname}注册成功`);
        }
    })
})


//商品列表接口
express.get("/getList", (request, response) => {
    sql.query(`select id,name,img,introduce,price from goods`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("商品列表页加载成功!");
            response.send(data);
        }
    })
})

//商品详情页刚进入页面接口
express.get("/details", (request, response) => {
    let id = request.query.id;
    sql.query(`select * from goods where id=${id}`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("商品详情页加载成功!");
            response.send(data);
        }
    })
})

//详情页-->加入购物车-->更新商品总数量和总价格接口
express.get("/allNum", (request, response) => {
    let id = request.query.id;
    let n = request.query.allnum;
    let p = request.query.allprice;
    sql.query(`update goods set allnum="${n}",allprice="${p}" where id="${id}"`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("详情页商品总数量和总价格更新成功!");
            response.send("success");
        }
    })
})


//详情页-->更换小图片接口
express.get("/simg", (request, response) => {
    let bigId = request.query.bigId;
    sql.query(`select smallImg from spicture where bigId=${bigId}`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("小图片更新成功!");
            response.send(data);
        }
    })
})


//购物车页面刚进入更新接口
express.get("/getShopping", (request, response) => {
    let id = request.query.id;
    sql.query(`select * from goods where id = "${id}" `, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("购物车信息加载成功!");
            response.send(data);
        }
    })
})

//购物车页面中点击减号更新接口
express.get("/numReduce", (request, response) => {
    let id = request.query.id;
    let n = request.query.allnum;
    let p = request.query.allprice;
    sql.query(`update goods set allnum="${n}",allprice="${p}" where id="${id}"`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("商品减少一件");
            response.send("success");
        }
    })
})

//购物车页面中点击加号更新接口
express.get("/numAdd", (request, response) => {
    let id = request.query.id;
    let n = request.query.allnum;
    let p = request.query.allprice;
    sql.query(`update goods set allnum="${n}",allprice="${p}" where id="${id}"`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("商品增加一件");
            response.send("success");
        }
    })
})


//订单页获取信息接口
express.get("/getOrder", (request, response) => {
    let id = request.query.id;
    sql.query(`select * from goods where id = "${id}" `, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("订单信息页加载成功!");
            response.send(data);
        }
    })
})

//订单页去支付接口
express.get("/success", (request, response) => {
    let q = request.query;
    sql.query(`insert into orders (allnum,allprice,goodsId,postName,postPhone,postAddress,date) values ("${q.allnum}","${q.allprice}","${q.goodsid}","${q.postname}","${q.postphone}","${q.postaddress}","${q.date}")`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            response.send("success");
        }
    })
})

//我的订单页更新数据
express.get("/getMyOrder", (request, response) => {
    let goodsId = request.query.goodsId;
    sql.query(`select orders.allnum,orders.allprice,orders.date,postAddress,postName,postPhone,img,goods.name from orders,goods where goodsId = "${goodsId}" and orders.goodsId=goods.id`, (error, data) => {
        if (error) {
            console.log(error);
            response.send("error");
        } else {
            console.log("我的订单页加载成功!");
            response.send(data);
        }
    })
})



//静态请求
express.use(expressStatic(__dirname + '/static'));

console.log(`running at ${port}`);
express.listen(port);